using System;
using System.Collections.Generic;
using System.Text;
using Widgetsphere.GeneratorEngine;

namespace Omniture.API.Generators.Objects
{
	internal class PluginTemplate : BaseTemplate
	{
		internal PluginTemplate(OmnitureProject project)
			: base(project)
		{
		}

		public override string FileContent
		{
			get
			{
				var sb = new StringBuilder();
				sb.AppendLine("using System;");
				sb.AppendLine("using System.Collections.Generic;");
				sb.AppendLine("using System.ComponentModel;");
				sb.AppendLine("using System.Text;");
				sb.AppendLine("using System.Web;");
				sb.AppendLine("using System.Web.UI;");
				sb.AppendLine("using System.Web.UI.WebControls;");
				sb.AppendLine("using Omniture.Plugin.Objects;");
				sb.AppendLine("using Omniture.API;");
				sb.AppendLine();
				sb.AppendLine("namespace Omniture.Plugin.Objects");
				sb.AppendLine("{");
				sb.AppendLine("}");
				sb.AppendLine();
				sb.AppendLine("namespace Omniture.Plugin");
				sb.AppendLine("{");
				sb.AppendLine();
				sb.AppendLine("	[DefaultProperty(\"Text\")]");
				sb.AppendLine("	[ToolboxData(\"<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>\")]");
				sb.AppendLine("	public class OmniturePlugin : WebControl");
				sb.AppendLine("	{");
				sb.AppendLine("		private PageCacheItem _pageConfiguration = null;");
				sb.AppendLine("		private bool _isLoaded = false;");
				sb.AppendLine("		private bool _isDebug = " + this.Project.IsDebug.ToString().ToLower() + ";");
				sb.AppendLine("		private bool _allowErrorOnPageNotFound = " + this.Project.AllowErrorOnPageNotFound.ToString().ToLower() + ";");
				sb.AppendLine();
				sb.AppendLine("		public OmniturePlugin()");
				sb.AppendLine("		{");
				sb.AppendLine("			_pageConfiguration = new PageCacheItem(this.Page);");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public OmniturePlugin(System.Web.UI.Page page)");
				sb.AppendLine("		{");
				sb.AppendLine("			_pageConfiguration = new PageCacheItem(page);");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public PageCacheItem PageConfiguration");
				sb.AppendLine("		{");
				sb.AppendLine("			get { return _pageConfiguration; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public bool AllowErrorOnPageNotFound");
				sb.AppendLine("		{");
				sb.AppendLine("			get { return _allowErrorOnPageNotFound; }");
				sb.AppendLine("			set { _allowErrorOnPageNotFound = value; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		/// <summary>");
				sb.AppendLine("		/// When true there is no script render on the page. This cancels all functionality of this control");
				sb.AppendLine("		/// </summary>");
				sb.AppendLine("		public bool IgnoreRender { get; set; }");
				sb.AppendLine();
				sb.AppendLine("		public bool IsDebug");
				sb.AppendLine("		{");
				sb.AppendLine("			get { return _isDebug; }");
				sb.AppendLine("			set { _isDebug = value; }");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		/// <summary>");
				sb.AppendLine("		/// Escape the quotes in Javascript strings");
				sb.AppendLine("		/// </summary>");
				sb.AppendLine("		private string EscapeString(string s)");
				sb.AppendLine("		{");
				sb.AppendLine("			s = s.Replace(((char)34).ToString(), ((char)92).ToString() + ((char)34).ToString());");
				sb.AppendLine("			return s;");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		protected override void OnInit(EventArgs e)");
				sb.AppendLine("		{");
				sb.AppendLine("			_pageConfiguration = new PageCacheItem(this.Page);");
				sb.AppendLine("			base.OnInit(e);");
				sb.AppendLine("			_isLoaded = _pageConfiguration.Load(this.Page.Request.Url.LocalPath);");
				sb.AppendLine("		}");
				sb.AppendLine();

				#region RenderContents

				//sb.AppendLine("		protected override void Render(HtmlTextWriter writer)");
				//sb.AppendLine("		{");
				//sb.AppendLine("			RenderContents (writer);");
				//sb.AppendLine("		}");
				//sb.AppendLine();
				//sb.AppendLine("		protected override void RenderContents(HtmlTextWriter output)");
				//sb.AppendLine("		{");
				//sb.AppendLine("		}");

				sb.AppendLine("		protected override void CreateChildControls()");
				sb.AppendLine("		{");
				sb.AppendLine();
				sb.AppendLine("			if (this.IgnoreRender)");
				sb.AppendLine("			{");
				sb.AppendLine("				base.CreateChildControls();");
				sb.AppendLine("				return;");
				sb.AppendLine("			}");
				sb.AppendLine();
				sb.AppendLine("			if(this.AllowErrorOnPageNotFound && !_isLoaded)");
				sb.AppendLine("			{");
				//sb.AppendLine("				throw new ApplicationException(\"Invalid Omniture configuration. Page not found: '\" + this.Page.Request.Url.LocalPath + \"'.\");");
				sb.AppendLine("				Literal lblError = new Literal();");
				sb.AppendLine("				lblError.Text = \"<strong><div style='color:Red;text-transform:uppercase;font-size:large;'>Invalid Omniture configuration. Page not found: '\" + this.Page.Request.Url.LocalPath + \"'.</div></strong>\";");
				sb.AppendLine("				this.Page.Controls.Add(lblError);");
				sb.AppendLine("			}");
				sb.AppendLine();
				sb.AppendLine("			var sb = new StringBuilder();");
				sb.AppendLine();
				sb.AppendLine("			if (this.IsDebug)");
				sb.AppendLine("			{");
				sb.AppendLine("				sb.AppendLine();");
				sb.AppendLine("				sb.AppendLine(\"<!-- \");");
				sb.AppendLine("				sb.AppendLine(\"** Omniture Plugin Debug **\");");
				sb.AppendLine("				sb.AppendLine(\"XML Configuration: \" + System.Configuration.ConfigurationManager.AppSettings[\"OmnitureSiteConfiguration\"]);");
				sb.AppendLine("				sb.AppendLine(\"ScriptCodeFile: \" + this.PageConfiguration.ScriptCodeFile);");
				sb.AppendLine("				sb.AppendLine(\"Current URL: \" + this.Page.Request.Url.LocalPath);");
				sb.AppendLine("				sb.AppendLine(\"-->\");");
				sb.AppendLine("			}");
				sb.AppendLine();
				//sb.AppendLine("			sb.AppendLine(\"<script language=\\\"JavaScript\\\" src=\\\"\" + this.PageConfiguration.ScriptCodeFile + \"\\\"></script>\");");
				sb.AppendLine("			if (this.PageConfiguration.ScriptCodeFile != \"\")");
				sb.AppendLine("				this.Page.ClientScript.RegisterClientScriptInclude(this.PageConfiguration.DocumentKey + \".omniture\", this.PageConfiguration.ScriptCodeFile);");
				sb.AppendLine("			sb.AppendLine(\"<script language=\\\"JavaScript\\\"><!--\");");
				sb.AppendLine();
				sb.AppendLine("			sb.AppendLine(\"" + this.Project.GlobalVariableName + ".pageName = \\\"\" + this.PageConfiguration.PageName + \"\\\";\");");
				sb.AppendLine("			sb.AppendLine(\"" + this.Project.GlobalVariableName + ".channel = \\\"\" + this.PageConfiguration.Channel + \"\\\";\");");
				sb.AppendLine("			if(this.PageConfiguration.Products != \"\")");
				sb.AppendLine("				sb.AppendLine(\"" + this.Project.GlobalVariableName + ".products = \\\"\" + this.PageConfiguration.Products + \"\\\";\");");
				sb.AppendLine("			if(this.PageConfiguration.TrackDownloadLinks)");
				sb.AppendLine("			{");
				sb.AppendLine("				sb.AppendLine(\"" + this.Project.GlobalVariableName + ".trackDownloadLinks = \" + this.PageConfiguration.TrackDownloadLinks.ToString().ToLower() + \";\");");
				sb.AppendLine("				sb.AppendLine(\"" + this.Project.GlobalVariableName + ".linkDownloadFileTypes = \\\"\" + this.PageConfiguration.LinkDownloadFileTypes + \"\\\";\");");
				sb.AppendLine("			}");
				sb.AppendLine("			if(this.PageConfiguration.TrackExternalLinks)");
				sb.AppendLine("			{");
				sb.AppendLine("				sb.AppendLine(\"" + this.Project.GlobalVariableName + ".trackExternalLinks = \" + this.PageConfiguration.TrackExternalLinks.ToString().ToLower() + \";\");");
				sb.AppendLine("				sb.AppendLine(\"" + this.Project.GlobalVariableName + ".linkInternalFilters = \\\"\" + this.PageConfiguration.LinkInternalFilters + \"\\\";\");");
				sb.AppendLine("			}");
				sb.AppendLine();

				sb.AppendLine("			//Generate Evars");
				foreach (ITrackItem item in this.Project.EVarList)
				{
					sb.AppendLine("			if ((this.PageConfiguration.Evars." + item.CodeName + " != null) && (this.PageConfiguration.Evars." + item.CodeName + " != \"\")) sb.AppendLine(\"" + this.Project.GlobalVariableName + "." + item.Name + " = \\\"\" + this.EscapeString(this.PageConfiguration.Evars." + item.CodeName + ") + \"\\\";\");");
				}
				sb.AppendLine();

				sb.AppendLine("			//Generate Props");
				foreach (ITrackItem item in this.Project.PropList)
				{
					sb.AppendLine("			if ((this.PageConfiguration.Props." + item.CodeName + " != null) && (this.PageConfiguration.Props." + item.CodeName + " != \"\")) sb.AppendLine(\"" + this.Project.GlobalVariableName + "." + item.Name + " = \\\"\" + this.EscapeString(this.PageConfiguration.Props." + item.CodeName + ") + \"\\\";\");");
				}
				sb.AppendLine();

				sb.AppendLine("			//Generate Events");
				sb.AppendLine("			string eventString = \"\";");
				foreach (var item in this.Project.EventList)
				{
					sb.AppendLine("			if (this.PageConfiguration.Events." + item.CodeName + " && this.PageConfiguration.Events." + item.CodeName + "SerializeInternal) eventString += \"" + item.Name + ":\" + GetEventSerializer(EventCache.EventCacheConstants." + item.CodeName + ") + \",\";");
					sb.AppendLine("			if (this.PageConfiguration.Events." + item.CodeName + " && !this.PageConfiguration.Events." + item.CodeName + "SerializeInternal) eventString += \"" + item.Name + ",\";");
				}
				sb.AppendLine("			if (eventString.EndsWith(\",\")) eventString = eventString.TrimEnd(',');");
				sb.AppendLine(@"			if (eventString != """") sb.AppendLine(@""" + this.Project.GlobalVariableName + @".events = """""" + eventString + @"""""";"");");
				sb.AppendLine();

				sb.AppendLine("			StringBuilder sbTemp = new StringBuilder();");
				if (!this.Project.CustomSubmit)
				{
					sb.AppendLine("			sb.AppendLine(\"var s_code=" + this.Project.GlobalVariableName + ".t();if(s_code)document.write(s_code);\");");
				}
				else
				{
					string script = this.Project.CustomSubmitScript.Replace("\r\n", "\n");
					string[] arr = script.Split('\n');
					foreach (string line in arr)
					{
						sb.AppendLine("			sbTemp.AppendLine(\"" + line.Replace("\"", @"\\""") + "\");");
					}
				}

				sb.AppendLine();
				sb.AppendLine("			sb.AppendLine(\"//--></script>\");");
				//sb.AppendLine("			output.Write(sb.ToString());");
				sb.AppendLine("			this.Page.ClientScript.RegisterClientScriptBlock(typeof(string), this.PageConfiguration.DocumentKey + \".omniture\", sb.ToString(), false);");
				sb.AppendLine("			this.Page.ClientScript.RegisterClientScriptBlock(typeof(string), this.PageConfiguration.DocumentKey + \".omniture2\", sbTemp.ToString(), true);");

				sb.AppendLine("			_wasRendered = true;");
				sb.AppendLine();
				sb.AppendLine("		}");
				sb.AppendLine();

				sb.AppendLine("		private string GetNewHash()");
				sb.AppendLine("		{");
				sb.AppendLine("			string secretKey = Guid.NewGuid().ToString();");
				sb.AppendLine("			string salt = \"123\";");
				sb.AppendLine("			var sha = System.Security.Cryptography.SHA1.Create();");
				sb.AppendLine("			byte[] preHash = System.Text.Encoding.UTF32.GetBytes(secretKey + salt);");
				sb.AppendLine("			byte[] hash = sha.ComputeHash(preHash);");
				sb.AppendLine("			string retval = System.Convert.ToBase64String(hash, 0, 15);");
				sb.AppendLine("			return retval;");
				sb.AppendLine("		}");

				//Event Serialization Helper Method
				sb.AppendLine("		private string GetEventSerializer(Omniture.Plugin.EventCache.EventCacheConstants eventItem)");
				sb.AppendLine("		{");
				sb.AppendLine("			switch (eventItem)");
				sb.AppendLine("			{");
				foreach (ITrackItem item in this.Project.EventList)
				{
					sb.AppendLine("				case EventCache.EventCacheConstants." + item.CodeName + ":");
					sb.AppendLine("					if (!this.PageConfiguration.Events." + item.CodeName + "SerializeInternal) return null;");
					sb.AppendLine("					if (this.PageConfiguration.Events." + item.CodeName + "SerializeKEYInternal != null)");
					sb.AppendLine("						return this.PageConfiguration.Events." + item.CodeName + "SerializeKEYInternal;");
					sb.AppendLine("					else return GetNewHash();");
				}
				sb.AppendLine("			}");
				sb.AppendLine("			return null;");
				sb.AppendLine("		}");
				sb.AppendLine();

				#endregion

				#region Unload Method

				sb.AppendLine("		private bool _wasRendered = false;");
				sb.AppendLine("		protected override void OnUnload(EventArgs e)");
				sb.AppendLine("		{");
				sb.AppendLine("			base.OnUnload(e);");
				sb.AppendLine("			if (_wasRendered)");
				sb.AppendLine("			{");
				sb.AppendLine("				if (HttpContext.Current.Session == null) return;");
				sb.AppendLine("				this.PageConfiguration.FormName = \"\";");
				sb.AppendLine("				this.PageConfiguration.PageName = \"\";");
				sb.AppendLine("				this.PageConfiguration.Products = \"\";");
				sb.AppendLine("				this.PageConfiguration.Channel = \"\";");
				sb.AppendLine("				this.PageConfiguration.Evars.Reset();");
				sb.AppendLine("				this.PageConfiguration.Props.Reset();");
				sb.AppendLine("				this.PageConfiguration.Events.Reset();");
				sb.AppendLine("			}");
				sb.AppendLine();
				sb.AppendLine("		}");
				sb.AppendLine();

				#endregion

				#region Render Methods

				sb.AppendLine("		public void RenderPage(HtmlTextWriter tempWriter, HtmlTextWriter pageWriter)");
				sb.AppendLine("		{");
				sb.AppendLine("			string html = tempWriter.InnerWriter.ToString();");
				if (this.Project.AllowPageRearrange)
				{
					sb.AppendLine();
					sb.AppendLine("			//Rearrange the VIEWSTATE");
					sb.AppendLine("			int StartPoint = html.IndexOf(\"<input type=\\\"hidden\\\" name=\\\"__VIEWSTATE\\\"\");");
					sb.AppendLine("			if (StartPoint >= 0)");
					sb.AppendLine("			{");
					sb.AppendLine("				int EndPoint = html.IndexOf(\"/>\", StartPoint) + 2;");
					sb.AppendLine("				string data = html.Substring(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				html = html.Remove(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				int FormEndStart = html.IndexOf(\"</form>\");");
					sb.AppendLine("				if (FormEndStart >= 0)");
					sb.AppendLine("				{");
					sb.AppendLine("					//then insert data");
					sb.AppendLine("					html = html.Insert(FormEndStart, data);");
					sb.AppendLine("				}");
					sb.AppendLine("			}");
					sb.AppendLine();
					sb.AppendLine("			//Rearrange the EVENTTARGET");
					sb.AppendLine("			StartPoint = html.IndexOf(\"<input type=\\\"hidden\\\" name=\\\"__EVENTTARGET\\\"\");");
					sb.AppendLine("			if (StartPoint >= 0)");
					sb.AppendLine("			{");
					sb.AppendLine("				int EndPoint = html.IndexOf(\"/>\", StartPoint) + 2;");
					sb.AppendLine("				string data = html.Substring(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				html = html.Remove(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				int FormEndStart = html.IndexOf(\"</form>\");");
					sb.AppendLine("				if (FormEndStart >= 0)");
					sb.AppendLine("				{");
					sb.AppendLine("					//then insert data");
					sb.AppendLine("					html = html.Insert(FormEndStart, data);");
					sb.AppendLine("				}");
					sb.AppendLine("			}");
					sb.AppendLine();
					sb.AppendLine("			//Rearrange the EVENTARGUMENT");
					sb.AppendLine("			StartPoint = html.IndexOf(\"<input type=\\\"hidden\\\" name=\\\"__EVENTARGUMENT\\\"\");");
					sb.AppendLine("			if (StartPoint >= 0)");
					sb.AppendLine("			{");
					sb.AppendLine("				int EndPoint = html.IndexOf(\"/>\", StartPoint) + 2;");
					sb.AppendLine("				string data = html.Substring(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				html = html.Remove(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				int FormEndStart = html.IndexOf(\"</form>\");");
					sb.AppendLine("				if (FormEndStart >= 0)");
					sb.AppendLine("				{");
					sb.AppendLine("					//then insert data");
					sb.AppendLine("					html = html.Insert(FormEndStart, data);");
					sb.AppendLine("				}");
					sb.AppendLine("			}");
					sb.AppendLine();
					sb.AppendLine("			//Rearrange the EVENTVALIDATION");
					sb.AppendLine("			StartPoint = html.IndexOf(\"<input type=\\\"hidden\\\" name=\\\"__EVENTVALIDATION\\\"\");");
					sb.AppendLine("			if (StartPoint >= 0)");
					sb.AppendLine("			{");
					sb.AppendLine("				int EndPoint = html.IndexOf(\"/>\", StartPoint) + 2;");
					sb.AppendLine("				string data = html.Substring(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				html = html.Remove(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				int FormEndStart = html.IndexOf(\"</form>\");");
					sb.AppendLine("				if (FormEndStart >= 0)");
					sb.AppendLine("				{");
					sb.AppendLine("					//then insert data");
					sb.AppendLine("					html = html.Insert(FormEndStart, data);");
					sb.AppendLine("				}");
					sb.AppendLine("			}			");
					sb.AppendLine();
					sb.AppendLine("			//Rearrange the TITLE");
					sb.AppendLine("			StartPoint = html.IndexOf(\"<title\");");
					sb.AppendLine("			if (StartPoint >= 0)");
					sb.AppendLine("			{				");
					sb.AppendLine("				int EndPoint = html.IndexOf(\"</title>\", StartPoint);");
					sb.AppendLine("				string data = html.Substring(StartPoint, EndPoint - StartPoint + 8);");
					sb.AppendLine("				html = html.Remove(StartPoint, data.Length);");
					sb.AppendLine("				int FormEndStart = html.IndexOf(\"<head\");");
					sb.AppendLine("				int FormBraceEnd = html.IndexOf(\">\", FormEndStart);");
					sb.AppendLine("				if (FormEndStart >= 0)");
					sb.AppendLine("				{");
					sb.AppendLine("					//then insert data");
					sb.AppendLine("					html = html.Insert(FormBraceEnd + 1, \"\\n\" + data);");
					sb.AppendLine("				}");
					sb.AppendLine("			}");
					sb.AppendLine();
					sb.AppendLine("			// Use the same method to move the javascript blocks inserted by .Net to the bottom too");
					sb.AppendLine("			StartPoint = html.IndexOf(\"<script type=\\\"text/javascript\\\">\\r\\n<!--\\r\\nvar theForm = document.forms[\\'aspnetForm\\']\");");
					sb.AppendLine("			if (StartPoint >= 0)");
					sb.AppendLine("			{");
					sb.AppendLine("				int EndPoint = html.IndexOf(\"</script>\", StartPoint) + 11;");
					sb.AppendLine("				string scriptInput = html.Substring(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				html = html.Remove(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				int FormEndStart = html.IndexOf(\"</form>\");");
					sb.AppendLine("				if (FormEndStart >= 0)");
					sb.AppendLine("				{");
					sb.AppendLine("					//In some cases the space between \"</form>\" will be missing so insert one");
					sb.AppendLine("					html = html.Insert(FormEndStart, \" \");");
					sb.AppendLine();
					sb.AppendLine("					//Then Insert ViewState Section");
					sb.AppendLine("					html = html.Insert(FormEndStart, scriptInput);");
					sb.AppendLine("				}");
					sb.AppendLine("			}");
					sb.AppendLine();
					sb.AppendLine("			//how many scripts are we going to move");
					sb.AppendLine("			int scriptCount = 0;");
					sb.AppendLine("			StartPoint = html.IndexOf(\"<script src=\\\"/WebResource.axd\");");
					sb.AppendLine("			while (StartPoint >= 0)");
					sb.AppendLine("			{");
					sb.AppendLine("				scriptCount++;");
					sb.AppendLine("				StartPoint = html.IndexOf(\"<script src=\\\"/WebResource.axd\", StartPoint + 1);");
					sb.AppendLine("			}");
					sb.AppendLine();
					sb.AppendLine("			//Now move the scripts");
					sb.AppendLine("			StartPoint = html.IndexOf(\"<script src=\\\"/WebResource.axd\");");
					sb.AppendLine("			for (int ii = 0; ii < scriptCount; ii++)");
					sb.AppendLine("			{");
					sb.AppendLine("				int EndPoint = html.IndexOf(\"</script>\", StartPoint) + 9;");
					sb.AppendLine("				string scriptInput = html.Substring(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				html = html.Remove(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				int FormEndStart = html.IndexOf(\"</form>\");");
					sb.AppendLine("				if (FormEndStart >= 0)");
					sb.AppendLine("				{");
					sb.AppendLine("					//In some cases the space between \"</form>\" will be missing so insert one");
					sb.AppendLine("					html = html.Insert(FormEndStart, \" \");");
					sb.AppendLine();
					sb.AppendLine("					//Then Insert ViewState Section");
					sb.AppendLine("					html = html.Insert(FormEndStart, scriptInput);");
					sb.AppendLine("				}");
					sb.AppendLine("				StartPoint = html.IndexOf(\"<script src=\\\"/WebResource.axd\", StartPoint + 1);");
					sb.AppendLine("			}");
					sb.AppendLine();
					sb.AppendLine("			//Look for and move the WebForm_OnSubmit function");
					sb.AppendLine("			StartPoint = html.IndexOf(\"<script type=\\\"text/javascript\\\">\\r\\n<!--\\r\\nfunction WebForm_OnSubmit\");");
					sb.AppendLine("			if (StartPoint >= 0)");
					sb.AppendLine("			{");
					sb.AppendLine("				int EndPoint = html.IndexOf(\"</script>\", StartPoint) + 11;");
					sb.AppendLine("				string scriptInput = html.Substring(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				html = html.Remove(StartPoint, EndPoint - StartPoint);");
					sb.AppendLine("				int FormEndStart = html.IndexOf(\"</form>\");");
					sb.AppendLine("				if (FormEndStart >= 0)");
					sb.AppendLine("				{");
					sb.AppendLine("					//In some cases the space between \"</form>\" will be missing so insert one");
					sb.AppendLine("					html = html.Insert(FormEndStart, \" \");");
					sb.AppendLine();
					sb.AppendLine("					//Then Insert ViewState Section");
					sb.AppendLine("					html = html.Insert(FormEndStart, scriptInput);");
					sb.AppendLine("				}");
					sb.AppendLine("			}");
					sb.AppendLine();
					sb.AppendLine("			//Rename the page form if necessary");
					sb.AppendLine("			if ((this.PageConfiguration.FormName != null) && (this.PageConfiguration.FormName != \"\"))");
					sb.AppendLine("			{");
					sb.AppendLine("				int i1 = html.IndexOf(\"<form\");");
					sb.AppendLine("				int i2 = html.IndexOf(\">\", i1 + 1);");
					sb.AppendLine("				if ((i1 >= 0) && (i2 >= 0))");
					sb.AppendLine("				{");
					sb.AppendLine("					string text = html.Substring(i1, i2 - i1 + 1);");
					sb.AppendLine("					text = text.Replace(\"name=\\\"aspnetForm\\\"\", \"name=\\\"\" + this.PageConfiguration.FormName + \"\\\"\");");
					sb.AppendLine("					text = text.Replace(\"id=\\\"aspnetForm\\\"\", \"id=\\\"\" + this.PageConfiguration.FormName + \"\\\"\");");
					sb.AppendLine("					html = html.Remove(i1, i2 - i1 + 1).Insert(i1, text); ;");
					sb.AppendLine("				}");
					sb.AppendLine("			}");
					sb.AppendLine();
					sb.AppendLine("			if (!string.IsNullOrEmpty(this.PageConfiguration.FormName))");
					sb.AppendLine("			{");
					sb.AppendLine("				html = html.Replace(\"document.forms['aspnetForm']\", \"document.forms['\" + this.PageConfiguration.FormName + \"']\");");
					sb.AppendLine("				html = html.Replace(\"document.aspnetForm\", \"document.\" + this.PageConfiguration.FormName + \"\");");
					sb.AppendLine("				html = html.Replace(\"getElementById('aspnetForm')\", \"getElementById('\" + this.PageConfiguration.FormName + \"')\");");
					sb.AppendLine("			}");
					sb.AppendLine();
				}
				sb.AppendLine("			pageWriter.Write(html);");
				sb.AppendLine("		}");
				sb.AppendLine();
				sb.AppendLine("		public static HtmlTextWriter GetPageTempWriter()");
				sb.AppendLine("		{");
				sb.AppendLine("			System.IO.StringWriter stringWriter = new System.IO.StringWriter();");
				sb.AppendLine("			HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);");
				sb.AppendLine("			return htmlWriter;");
				sb.AppendLine("		}");

				#endregion

				sb.AppendLine("	}");
				sb.AppendLine("}");
				return sb.ToString();
			}
		}

		public static void Generate(OmnitureProject project, string destinationFolder)
		{
			PluginTemplate t = new PluginTemplate(project);
			t.Generate(destinationFolder);
		}

		public override string FileName
		{
			get { return "OmniturePlugin.cs"; }
		}

	}
}